﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MapRoom
{
    class MapPoint
    {
        private double _x;
        private double _y;
        public double getX( ) { return _x; }
        public double getY( ) { return _y; }
        public MapPoint( double x, double y ) { _x = x; _y = y; }
    }

    class MapModel
    {
        private RobotPOIData _myData;
        private LinkedList<MapPoint> _mapPoints;

        public void resetData( )
        {
            _mapPoints = new LinkedList<MapPoint>( );
        }

        // called whenever we receive an update from the robot over the serial port
        public void updateMapModel( MapModelUpdateData mapModelUpdateData )
        {
            // did the robot tell us that he's starting over (or just starting)?
            if( mapModelUpdateData.inResetMode )
            {
            }

            // did the robot tell us he's in scan mode (i.e. spinning around in place)?
            if( mapModelUpdateData.inSpinMode )
            {
                double xDistance = mapModelUpdateData.forwardMeasurement
                    * ( Math.Sin( ( mapModelUpdateData.approximateAngle - 90 ) * 2 * Math.PI / 360.0 ) );
                double yDistance = mapModelUpdateData.forwardMeasurement
                    * ( Math.Cos( ( mapModelUpdateData.approximateAngle - 90 ) * 2 * Math.PI / 360.0 ) );
                _mapPoints.AddLast( new MapPoint( xDistance, yDistance ) );
                _myData.robotAngle = mapModelUpdateData.approximateAngle;
            }
            else
            {
            }
        }
        
        public RobotPOIData getRobotPOIData( )
        {
            //Random myRandom = new Random( );
            //_myData.roomHeight += myRandom.Next( 3 ) - 1;
            //_myData.roomWidth += myRandom.Next( 3 ) - 1;
            //if( _myData.roomHeight < 5 ) _myData.roomHeight = 5;
            //if( _myData.roomWidth < 5 ) _myData.roomWidth = 5;
            //_myData.robotLocationX += myRandom.Next( 3 ) - 1;
            //_myData.robotLocationY += myRandom.Next( 3 ) - 1;
            //if( _myData.robotLocationX < 0 ) _myData.robotLocationX = 0;
            //if( _myData.robotLocationX > _myData.roomWidth ) _myData.robotLocationX = _myData.roomWidth;
            //if( _myData.robotLocationY < 0 ) _myData.robotLocationY = 0;
            //if( _myData.robotLocationY > _myData.roomHeight ) _myData.robotLocationY = _myData.roomHeight;
            //_myData.robotAngle+=10;
            //if( _myData.robotAngle > 359 ) _myData.robotAngle -= 360;
            return _myData;    
        }

        public LinkedList<MapPoint> getMapPoints( )
        {
            return _mapPoints;
        }

        public MapModel( )
        {
            _myData = new RobotPOIData( );
            _myData.roomWidth = 10;
            _myData.roomHeight = 10;
            _myData.robotLocationX = 5;
            _myData.robotLocationY = 5;
            _myData.doorX = 1;
            _myData.doorY = 0;
            _myData.doorWidth = 3;
            _myData.doorHeight = 0;
            _myData.robotAngle = 0;
            _mapPoints = new LinkedList<MapPoint>( );
        }
    }
}
